package com.hundsun.cprs.yyt.gateway.service.uc.impl;

import com.hundsun.cprs.yyt.gateway.enums.EnumFundBankNo;
import com.hundsun.cprs.yyt.service.common.dto.FundCloseInfo;
import org.apache.commons.lang.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hundsun.cprs.yyt.common.enums.EnumBusinessCode;
import com.hundsun.cprs.yyt.gateway.response.BaseResponse;
import com.hundsun.cprs.yyt.gateway.response.RealNameResponse;
import com.hundsun.cprs.yyt.gateway.service.uc.BankBindingService;
import com.hundsun.cprs.yyt.service.common.dto.FundBankChannelVerificateRequest;
import com.hundsun.cprs.yyt.service.common.dto.FundCommResponse;
import com.hundsun.cprs.yyt.service.common.dto.FundOpenInfo;
import com.hundsun.cprs.yyt.service.common.service.RemoteTradeFundService;

/**
 * 环迅三方支付渠道
 * @author linrl
 * @date 2018年3月28日
 */
@Service
public class HxpayBankBindingServiceImpl implements BankBindingService {
    
    @Autowired
    private RemoteTradeFundService remoteTradeFundService;
    
    @Override
    public String getBankNo() {
        return EnumFundBankNo.HXPAY.getCode();
    }

    @Override
    public void bindingValidate(FundOpenInfo request,BaseResponse response) {
        //TODO:委托给资金系统校验填写的银行卡信息，校验通过，则发送客户短信
        Validate.notEmpty(request.getChannelBankNo(),"银行产品子渠道必填");
        Validate.notEmpty(request.getChannelBankNo(),"银行编号必填");
        FundBankChannelVerificateRequest req = new FundBankChannelVerificateRequest();
        req.setBankChannelNo(request.getChannelBankNo());
        req.setBankClientNo(request.getBankClientNo());
        req.setFundAccount(request.getFundAccount());
        req.setMobile(request.getMobile());
        req.setBankAccount(request.getBankAccount());
        req.setVerifyType("0");
        remoteTradeFundService.sendAndGetBankChannelVerificateCode(req);
        
        //校验通过，且短信发送成功后，以特殊错误码3015返回给前端，做下一步操作。
        response.setErrorNo(EnumBusinessCode.BUSSNESS_3015.getErrorNo());
        response.setErrorInfo(EnumBusinessCode.BUSSNESS_3015.getErrorInfo());
        response.setSuccess(true);
    }

    @Override
    public void binding(FundOpenInfo request,BaseResponse response) {
        //签约成功后，则发起环迅快捷的开户动作
        FundCommResponse resp = remoteTradeFundService.open(request);
        
        if(response instanceof RealNameResponse){
            RealNameResponse innerResp = (RealNameResponse)response;
            innerResp.setErrorNo(resp.getCode());
            innerResp.setErrorInfo(resp.getDesc());
            innerResp.setSuccess(true);
            innerResp.setRedirectUrl(resp.getAttachment());
            innerResp.setRequestContent(resp.getAttachmentDetail());
        }
    }

    @Override
    public void unBinding(FundCloseInfo request, BaseResponse response) {
        remoteTradeFundService.close(request);
    }

}
